/*
 * DirectoryRestrictedFileSystemView.java
 *
 *  created: 10.10.2011
 *  charset: UTF-8
 */
package cz.mp.k3bg.misc;

import java.io.File;
import java.io.IOException;
import javax.swing.filechooser.FileSystemView;

/**
 * 
 * <a href="http://stackoverflow.com/questions/32529/how-do-i-restrict-jfilechooser-to-a-directory">
 * http://stackoverflow.com/questions/32529/how-do-i-restrict-jfilechooser-to-a-directory</a>
 * 
 *
 */
public class DirectoryRestrictedFileSystemView extends FileSystemView {
    private final File[] rootDirectories;

    public DirectoryRestrictedFileSystemView(File rootDirectory) {
        this.rootDirectories = new File[] {rootDirectory};
    }

    public DirectoryRestrictedFileSystemView(File[] rootDirectories) {
        this.rootDirectories = rootDirectories;
    }

    @Override
    public File createNewFolder(File containingDir) throws IOException
    {       
        throw new UnsupportedOperationException("Unable to create directory");
    }

    @Override
    public File[] getRoots()
    {
        return rootDirectories;
    }

    @Override
    public boolean isRoot(File file)
    {
        for (File root : rootDirectories) {
            if (root.equals(file)) {
                return true;
            }
        }
        return false;
    }
    
}   // DirectoryRestrictedFileSystemView.java

